var index = parent.layer.getFrameIndex(window.name);
var startSetting = parent.$("#startSetting").val();
var otherC_value = parent.$("#other").val();
var datas = parent.$("#datas").val();
var json_datas = (new Function('return' + datas))();
var task_number = parent.$("#taskNumber").val();
$("#showTaskNum").html(task_number);
var task_number_origin = parent.$("#taskNumber").val();
var one_task = "";
var one_task_file = 0;
var one_task_dict = 0;
let one_task_data_num = 0;
let one_task_input_num = 0;
var input_value = parent.$("#input").val();
var allowModification = parent.$("#allowModification").val();
var DHtml_all = 0;
var DHtml_index = 0;
var DHtml_content;
var all_value;
let all_value_back;
window.onload = function () {
    $.ajax({
        url: constUrl + "AlgorithmServlet",
        type: 'post',
        dataType: 'json',
        data: {"operateType": "packBlockDetail", "data": startSetting},
        success: function (responseText) {
            var jsonData = responseText;
            $("#task_number").val(task_number);
            $("#showTaskNum").html(task_number);
            $("#taskNum").html(task_number);
            var other_form = document.getElementById("other_form");
            other_form.innerHTML += '<div class="form-title" style="text-align: center">' + '<h4>其他配置</h4>' + '</div>';
            other_form.innerHTML += '<hr>' + '<div class="other">' + '<div class="form-group"> ' + '<label class="col-sm-2">算法优先级:</label> ' + '</div> ' + '<input type="number" min="1" max="100" class="form-group form-control" id="otherPriority" value="50" onblur="checkInt(this.value,100);"> ' + '</div> ' + '<hr>' + '<div class="other">' + '<div class="form-group"> ' + '<label class="col-sm-2">并行策略:</label> ' + '</div> ' + '<select class="form-group form-control sl" id="otherParallelStrategy">' + '<option value="1" disabled="disabled">请选择策略</option>' + '<option value="ut" selected = "selected">按CPU可利用率最大</option>' + '<option value="-ut" >按CPU可利用率最小</option>' + '<option value="mem" >按内存剩余最大</option>' + '<option value="-mem" >按内存剩余最小</option>' + '</select>' + '</div> ' + '<hr>' + '<div class="other">' + '<div class="form-group"> ' + '<label class="col-sm-2">节点分组:</label> ' + '</div> ' + '<select class="form-group form-control sl" id="otherHostGroup">' + '<option value="1" disabled="disabled">请选择节点分组</option>' + '</select>' + '</div> ';
            other_form.innerHTML += '<div style="clear:both"></div>';
            $.ajax({
                url: constUrl + "HostGroupList",
                type: 'post',
                dataType: 'json',
                data: {method: "getAllGroup"},
                success: function (hgl_responseText) {
                    if (hgl_responseText.result == "success") {
                        var group = '';
                        for (var i = 0; i < hgl_responseText.message.length; i++) {
                            group += '<option value="' + (hgl_responseText.message[i].GROUP_NAME) + '" >' + hgl_responseText.message[i].GROUP_NAME + '</option>'
                        }
                        $("#otherHostGroup").append(group);
                    }
                },
                complete: function () {
                },
                error: function () {
                }
            });
            if (otherC_value != "" && otherC_value != "undefined") {
                var otherC_json = (new Function('return' + otherC_value))();
                $("#otherPriority").val(otherC_json[0].Priority);
                $("#otherParallelStrategy").val(otherC_json[0].ParallelStrategy);
            }
            if (input_value != null && input_value !== "" && input_value !== "[]") {
                all_value = (new Function('return' + input_value))();
                all_value_back = createAllValue(jsonData);
            } else {
                all_value = createAllValue(jsonData);
                all_value_back = cloneObj(all_value);
            }
            getDocument("");
            if (allowModification == "false") {
                $("#y-title").append("--查看模式");
                $(":button").attr("disabled", true);
                $("input[type=text]").attr("readonly", true);
            }
            let add_page_num = Math.ceil(all_value.length / 10);
            addPageNum(add_page_num);
            addBox(1, 10)
            paginationInit();
        },
        complete: function () {
        },
        error: function () {
            layer.alert("获取数据失败！")
        }
    });
    $('#saveSetting').click(function () {
        if (allowModification == "true") {
            let json_input_save = JSON.stringify(all_value);
            window.parent.document.getElementById("input").value = json_input_save;
            var otherConfig = [];
            otherConfig.push({
                Priority: $("#otherPriority").val(),
                ParallelStrategy: $("#otherParallelStrategy").val(),
                HostGroup: $("#otherHostGroup").val()
            });
            var json_otherConfig = JSON.stringify(otherConfig);
            window.parent.document.getElementById("other").value = json_otherConfig;
            if (task_number_origin != task_number) {
                $(".uiAlgorithm", window.parent.document).each(function (index, elem) {
                    var $elem = $(elem);
                    $elem.children(".uidiv").children(".progressbar").children(".progress-bar").attr("style", "width: 0%");
                });
                window.parent.document.getElementById("taskNumber").value = task_number;
                window.parent.document.getElementById("showTaskNum").innerHTML = task_number;
            }
            parent.layer.close(index);
        } else {
            layer.alert("查看模式无法进行该操作！");
        }
    });
    $('#closeIframe').click(function () {
        parent.layer.close(index);
    });
    $("#input_form").on("click", ".input_input", function () {
        if (allowModification == "true") {
            event = arguments.callee.caller.arguments[0] || window.event;
            if (event.shiftKey == 1 && $("#sign_task").val() != "" && $("#sign_task").val() != null) {
                if (parseInt($("#isDict").val()) == 1){
                    let o_input = $("#sign_input").val();
                    let o_task = $("#sign_task").val();
                    let n_task = $(this).attr("all-where");
                    let n_input = $(this).attr("current-where");
                    if (o_task == n_task) {
                        if (o_input != n_input) {
                            let resultAll = "";
                            if (n_input > o_input) {
                                for (let i = parseInt(o_input); i < parseInt(n_input) + 1; i++) {
                                    let input_temp = $(this).parent().parent().parent().parent().children(".tr_input").eq(i).children(".td_input").children(".am-form-group").children("input");
                                    if (input_temp.attr("input-type") == "file" && input_temp.val() == "") {
                                        resultAll += i + ",";
                                        input_temp.addClass("input_checked");
                                        input_temp.val("");
                                        input_temp.attr("title", "");
                                    }
                                }
                            } else {
                                layer.alert("请不要反向多选！");
                            }
                            resultAll = resultAll.substring(0, resultAll.length - 1);
                            $("#multiSign").val(resultAll);
                            $("#sign_input").val("");
                        }
                    } else {
                        layer.alert("请不要跨任务多选！");
                    }
                } else {
                    layer.alert("请不要多选文件夹输入框！");
                }
            } else if (event.ctrlKey == 1 && (($("#sign_task").val() != "" && $("#sign_task").val() != null) || ($("#multiSign").val() != "" && $("#multiSign").val() != null))) {
                if (parseInt($("#isDict").val()) == 1){
                    let n_input = $(this).attr("current-where");
                    let n_task = $(this).attr("all-where");
                    let o_task = $("#sign_task").val();
                    if (o_task == n_task) {
                        if ($("#sign_task").val() != "" && $("#sign_task").val() != null) {
                            if ($(this).attr("input-type") == "directory"){
                                layer.alert("请不要多选文件夹输入框！");
                            } else {
                                let all_input = $("#multiSign").val();
                                if (all_input.indexOf(n_input) == -1) {
                                    $(this).addClass("input_checked");
                                    $(this).val("");
                                    $(this).attr("title", "");
                                    $("#multiSign").val($("#sign").val() + "," + n_input);
                                    $("#sign_input").val(n_input);
                                } else {
                                    $(this).removeClass("input_checked");
                                    $(this).val("");
                                    $(this).attr("title", "");
                                    all_input = all_input.replace(n_input + ",", "");
                                    all_input = all_input.replace("," + n_input, "");
                                    $("#multiSign").val(all_input);
                                    $("#sign_input").val(n_input);
                                }
                            }
                        } else {
                            if ($(this).val() == "") {
                                $(".input_input").each(function (index, elem) {
                                    var $elem = $(elem);
                                    $elem.removeClass("input_checked");
                                });
                                if ($(this).attr("input-type") == "directory") {
                                    $("#isDict").val('1');
                                } else {
                                    $("#isDict").val('0');
                                }
                                $("#sign_task").val($(this).attr("all-where"));
                                $("#sign_input").val($(this).attr("current-where"));
                                $("#multiSign").val("");
                                $(this).addClass("input_checked");
                            }
                        }
                    } else {
                        layer.alert("请不要跨任务多选！");
                    }
                } else {
                    layer.alert("请不要多选文件夹输入框！");
                }
            } else {
                if ($(this).val() != "" && $(this).parent().children(".del-icon").css("display") == "none") {
                    $(this).css("padding-left", "21px");
                    $(this).parent().children(".del-icon").css("display", "block");
                } else if ($(this).val() != "" && $(this).parent().children(".del-icon").css("display") == "block") {
                    $(this).parent().children(".del-icon").css("display", "none");
                    $(this).css("padding-left", "0");
                } else {
                    $(".input_input").each(function (index, elem) {
                        var $elem = $(elem);
                        $elem.removeClass("input_checked");
                    });
                    if ($(this).attr("input-type") == "directory") {
                        $("#isDict").val('1');
                    } else {
                        $("#isDict").val('0');
                    }
                    $("#sign_task").val($(this).attr("all-where"));
                    $("#sign_input").val($(this).attr("current-where"));
                    $("#multiSign").val("");
                    $(this).addClass("input_checked");
                }
            }
        } else {
            layer.alert("查看模式无法进行该操作！");
        }
    });
    $("#input_form").on("click", ".del-icon", function () {
        if (allowModification == "true") {
            $(this).parent().children(".input_input").val("");
            $(this).parent().children(".input_input").attr("title", "");
            $(this).parent().children(".input_input").css("padding-left", "0");
            $(this).css("display", "none");
            let current_task = parseInt($(this).parent().children(".input_input").attr("all-where"));
            let current_input = parseInt($(this).parent().children(".input_input").attr("current-where"));
            if (current_input < one_task_input_num){
                all_value[current_task]["input"][current_input]["frontOutput"]["frontOutputName"] = "";
            } else {
                all_value[current_task]["data"][current_input-one_task_input_num]["dataValue"] = "";
            }
        } else {
            layer.alert("查看模式无法进行该操作！");
        }
    });
    $('#addTask').click(function () {
        if (allowModification == "true") {
            task_number++;
            $("#task_number").val(task_number);
            $("#showTaskNum").html(task_number);
            let temp_task = cloneObj(one_task)
            all_value.push(temp_task);
            let add_page_num = Math.ceil(all_value.length / 10);
            let c_page_number = $("#test1").attr('pagenumber');
            addPageNum(add_page_num);
            addBox(c_page_number, 10);
            paginationInit();
        } else {
            layer.alert("查看模式无法进行该操作！");
        }
    });
    /* 引用文件 将左侧文件依次填入到右侧选中的多个输入框中。*/
    $("#quoteButton").click(function () {
        if (allowModification == "true") {
            let multiSign = $("#multiSign").val();
            if (multiSign != "" && multiSign != null) {
                let file_num = $("#div_document").children(".div_choiceDocument").length;
                if (file_num > 0) {
                    let all_input = multiSign.split(',');
                    let input_num = all_input.length;
                    let one_task = parseInt($("#sign_task").val());
                    let temp_url = $("#url").val();
                    let url = temp_url.substring(1, temp_url.length);
                    let min_num = 0;
                    if (file_num > input_num) {
                        min_num = input_num;
                    } else {
                        min_num = file_num;
                    }
                    for (let i = 0; i < min_num; i++) {
                        let allDocumentName = url + $("#div_document").children(".div_choiceDocument").eq(i).html();
                        let input_temp = $("#table").children("tbody").eq(one_task % 10).children(".tr_input").eq(i).children(".td_input").children(".am-form-group").children("input");
                        input_temp.val(allDocumentName);
                        input_temp.attr("title", allDocumentName);
                        if (i < one_task_input_num){
                            all_value[one_task]["input"][i]["frontOutput"]["frontOutputName"] = allDocumentName;
                        } else {
                            all_value[one_task]["data"][i-one_task_input_num]["dataValue"] = allDocumentName;
                        }
                    }
                    $("#multiSign").val("");
                    $("#sign_task").val("");
                    $("#sign_input").val("");
                    $(".input_input").each(function (index, elem) {
                        let $elem = $(elem);
                        $elem.removeClass("input_checked");
                    });
                } else {
                    layer.msg("请确认当前文件夹中有文件！");
                }
            } else {
                layer.msg("请选择多个输入框！");
            }
        } else {
            layer.alert("查看模式无法进行该操作！");
        }
    });
    /* 动态全选 原有值会被覆盖
    将左侧所有文件填入到右侧选中的多个输入框开始的任务往下所有任务对应的多个输入框中
    若文件存在多余则不进行额外操作
    若文件不足，则从头循环文件，直至所有需要填入的输入框都填入了值
    */
    $("#allButton").click(function () {
        if (allowModification == "true") {
            let current_task = $("#sign_task").val();
            let current_multi = $("#multiSign").val();
            if (current_multi != "" && current_multi != null && current_task != "" && current_task != null) {
                let file_num = $("#div_document").children(".div_choiceDocument").length;
                if (file_num > 0) {
                    let one_task_all_input = current_multi.split(',');
                    let one_input_num = one_task_all_input.length;
                    let all_input_num = one_input_num * (task_number - parseInt(current_task));
                    let x = parseInt(current_task) - 1;
                    for (let i = 0; i < all_input_num; i++){
                        let z = i % file_num;
                        let allDocumentName = url + $("#div_document").children(".div_choiceDocument").eq(z).html();
                        let y_temp = i % one_input_num;
                        if (y_temp == 0)
                            x += 1;
                        let y = current_multi[y_temp];
                        if (y < one_task_input_num){
                            all_value[x]["input"][y]["frontOutput"]["frontOutputName"] = allDocumentName;
                        } else {
                            all_value[x]["data"][y-one_task_input_num]["dataValue"] = allDocumentName;
                        }
                    }
                    let c_page_number = $("#test1").attr('pagenumber');
                    addBox(c_page_number, 10);
                    $("#multiSign").val("");
                    $("#sign_task").val("");
                    $("#sign_input").val("");
                } else {
                    layer.msg("请确认当前文件夹中有文件！");
                }
            } else {
                layer.msg("请选择多个输入框！");
            }
        } else {
            layer.alert("查看模式无法进行该操作！");
        }
    });
    /* 一键配置 原有值会被覆盖
    没有选中任一输入框时，将左侧文件依次填入到右侧所有输入框中。
    选中了一个输入框，则是将左侧文件依次填入到右侧选中的输入框开始往下的所有输入框中。
    待填入文件数量或输入框数量不足都不进行处理。
    */
    $('#setButton').click(function () {
        if (allowModification === "true") {
            let current_input = $("#sign_input").val();
            let current_task = $("#sign_task").val();
            let current_multi = $("#multiSign").val();
            if (current_multi == "" || current_multi == null) {
                let file_num = $("#div_document").children(".div_choiceDocument").length;
                if (file_num > 0){
                    let all_none_input;
                    let x = 0;
                    let y = 0;
                    if (current_input != "" && current_input !== null) {
                        //选择了一个输入框
                        x = parseInt(current_task);
                        y = parseInt(current_input);
                        all_none_input = (task_number - (x + 1)) * (one_task_input_num + one_task_data_num) + (one_task_data_num + one_task_input_num - y);
                    } else {
                        all_none_input = task_number * (one_task_input_num + one_task_data_num);
                    }
                    let temp_url = $("#url").val();
                    let url = temp_url.substring(1, temp_url.length);
                    let min_num = 0;
                    if (file_num > all_none_input) {
                        min_num = all_none_input;
                    } else {
                        min_num = file_num;
                    }
                    for (let i = 0; i < min_num; i++){
                        let allDocumentName = url + $("#div_document").children(".div_choiceDocument").eq(i).html();
                        if (y < one_task_input_num){
                            all_value[x]["input"][y]["frontOutput"]["frontOutputName"] = allDocumentName;
                        } else {
                            all_value[x]["data"][y-one_task_input_num]["dataValue"] = allDocumentName;
                        }
                        y += 1;
                        if (y === one_task_data_num + one_task_input_num){
                            y = 0;
                            x += 1;
                        }
                    }
                    let c_page_number = $("#test1").attr('pagenumber');
                    addBox(c_page_number, 10);
                    $("#sign_input").val("");
                    $("#sign_task").val("");
                    $("#multiSign").val("");
                } else {
                    layer.msg("请确认当前文件夹中有文件！");
                }
            } else {
                layer.alert("当前选择了多个输入框，无法完成一键配置操作！");
            }
        } else {
            layer.alert("查看模式无法进行该操作！");
        }
    });
    /* 文件全选 原有值会被覆盖
    将左侧所有文件填入到右侧选中的多个输入框开始的任务往下所有任务对应的多个输入框中
    若文件存在多余，则创建新的任务进行填入
    文件不足则不进行额外操作
    */
    $('#allFile').click(function () {
        if (allowModification == "true") {
            let current_task = $("#sign_task").val();
            let current_multi = $("#multiSign").val();
            if (current_multi != "" && current_multi != null && current_task != "" && current_task != null) {
                let file_num = $("#div_document").children(".div_choiceDocument").length;
                if (file_num > 0) {
                    let one_task_all_input = current_multi.split(',');
                    let one_input_num = one_task_all_input.length;
                    let x = parseInt(current_task) - 1;
                    for (let i = 0; i < file_num; i++){
                        let allDocumentName = url + $("#div_document").children(".div_choiceDocument").eq(z).html();
                        let y_temp = i % one_input_num;
                        if (y_temp == 0)
                            x += 1;
                        if (x == all_value.length){
                            task_number++;
                            let temp_task = cloneObj(one_task);
                            all_value.push(temp_task);
                        }
                        let y = current_multi[y_temp];
                        if (y < one_task_input_num){
                            all_value[x]["input"][y]["frontOutput"]["frontOutputName"] = allDocumentName;
                        } else {
                            all_value[x]["data"][y-one_task_input_num]["dataValue"] = allDocumentName;
                        }
                    }
                    $("#task_number").val(task_number);
                    $("#showTaskNum").html(task_number);
                    let add_page_num = Math.ceil(all_value.length / 10);
                    let c_page_number = $("#test1").attr('pagenumber');
                    addPageNum(add_page_num);
                    addBox(c_page_number, 10);
                    paginationInit();
                    $("#multiSign").val("");
                    $("#sign_task").val("");
                    $("#sign_input").val("");
                } else {
                    layer.msg("请确认当前文件夹中有文件！");
                }
            } else {
                layer.msg("请选择多个输入框！");
            }
        } else {
            layer.alert("查看模式无法进行该操作！");
        }
    });
    $('#deleteButton').click(function () {
        if (allowModification == "true") {
            for (let i = 0; i < all_value.length; i++){
                let temp_task = all_value[i];
                for (let x = 0; x < temp_task["input"]; x++){
                    temp_task["input"][x]["frontOutput"]["frontOutputName"] = "";
                }
                for (let y = 0; y < temp_task["data"]; y++){
                    temp_task["data"][y]["dataValue"] = "";
                }
            }
            let c_page_number = $("#test1").attr('pagenumber');
            addBox(c_page_number, 10);
            $("#sign_input").val("");
            $("#sign_task").val("");
            $("#multiSign").val("");
        } else {
            layer.alert("查看模式无法进行该操作！");
        }
    });
}
createAllValue = function (jsonData) {
    let data = [];
    let one_input = [];
    let result = [];
    one_task_file = 0;
    one_task_dict = 0;
    one_task_data_num = json_datas.length;
    one_task_input_num = jsonData[0].algorithmInputs.length;
    for (let i = 0; i < json_datas.length; i++) {
        if (json_datas[i].type == "file") {
            one_task_file++;
        } else {
            one_task_dict++;
        }
        let temp = {
            type: json_datas[i].type,
            dataBlockName: json_datas[i].Name,
            dataName: json_datas[i].dataName + '+' + json_datas[i].dataBlockId + '+' + json_datas[i].dataNumber,
            dataValue: ""
        }
        data.push(temp);
    }
    for (let j = 0; j < jsonData.length; j++) {
        let inputConfig = jsonData[j].algorithmInputs;
        for (let i = 0; i < inputConfig.length; i++) {
            if (inputConfig[i].type == "file") {
                one_task_file++;
            } else {
                one_task_dict++;
            }
            let temp = {
                type: inputConfig[i].type,
                blockId: jsonData[j].blockId,
                blockName: jsonData[j].blockName,
                inputName: inputConfig[i].inputName,
                frontOutput: {frontOutputName: "", blockId: "uiStart_1", dataId: ""}
            };
            one_input.push(temp);
        }
    }
    one_task = {input: one_input, data: data}
    for (let o = 0; o < task_number; o++) {
        let temp_task = cloneObj(one_task);
        result.push(temp_task)
    }
    return result;
}
returnPrevious = function () {
    var url = $("#url");
    var urlHtml = url.val();
    var urlHtml_one = urlHtml.split("/");
    urlHtml = "";
    if (urlHtml_one.length > 2) {
        for (var i = 0; i < urlHtml_one.length - 2; i++) {
            urlHtml += urlHtml_one[i] + "/";
        }
    } else {
        urlHtml += "/";
    }
    url.val(urlHtml);
    urlHtml = urlHtml.substring(1, urlHtml.length);
    if (urlHtml == "/") urlHtml = "";
    $.ajax({
        url: constUrl + "GetFilePath",
        type: 'post',
        dataType: 'json',
        data: {"url": urlHtml},
        success: function (responseText) {
            if (responseText.result) {
                var jsonData = responseText;
                createDHtml(jsonData.message);
            } else {
                layer.alert("获取数据失败，请重试！");
            }
        },
        complete: function () {
        },
        error: function () {
            layer.alert("获取数据失败！");
        }
    });
}
getDocument = function (DocumentName) {
    if (parseInt($("#isDict").val()) == 1) {
        let temp_url = $("#url").val();
        let url = temp_url.substring(1, temp_url.length);
        let allDocumentName = url + DocumentName;
        let d_input = $("#sign_input").val();
        let d_task = $("#sign_task").val();
        if (d_input != "" && d_input !=null && d_task != "" && d_task != null){
            if (d_input < one_task_input_num){
                all_value[d_task]["input"][d_input]["frontOutput"]["frontOutputName"] = allDocumentName;
            } else {
                all_value[d_task]["data"][d_input-one_task_input_num]["dataValue"] = allDocumentName;
            }
            let input_temp = $("#table").children("tbody").eq(d_task % 10).children(".tr_input").eq(d_input).children(".td_input").children(".am-form-group").children("input");
            input_temp.val(allDocumentName);
            input_temp.attr("title", allDocumentName);
        } else {
            layer.alert("文件夹输入框选择出错，请重新选择输入框，或重新刷新页面！");
        }
    } else {
        var url = $("#url");
        url.val(url.val() + DocumentName + "/");
        var urlHtml = url.val();/* 删除第一个"/"*/
        urlHtml = urlHtml.substring(1, urlHtml.length);
        $.ajax({
            url: constUrl + "GetFilePath",
            type: 'post',
            dataType: 'json',
            data: {"url": urlHtml},
            success: function (responseText) {
                if (responseText.result) {
                    var jsonData = responseText;
                    createDHtml(jsonData.message);
                } else {
                    layer.alert("获取数据失败，请重试！");
                }
            },
            complete: function () {
            },
            error: function () {
                layer.alert("获取数据失败！")
            }
        });
    }
}
choiceDocument = function (DocumentName) {
    var temp_url = $("#url").val();
    var url = temp_url.substring(1, temp_url.length);
    var allDocumentName = url + DocumentName;
    let current_input = $("#sign_input").val();
    let current_task = $("#sign_task").val();
    if (current_input != "" && current_input != null && current_task != "" && current_task != null) {
        if (parseInt($("#isDict").val()) == 0) {
            let d_input = parseInt(current_input);
            let d_task = parseInt(current_task);
            if (d_input < one_task_input_num){
                all_value[d_task]["input"][d_input]["frontOutput"]["frontOutputName"] = allDocumentName;
            } else {
                all_value[d_task]["data"][d_input-one_task_input_num]["dataValue"] = allDocumentName;
            }
            let input_temp = $("#table").children("tbody").eq(d_task % 10).children(".tr_input").eq(d_input).children(".td_input").children(".am-form-group").children("input");
            input_temp.val(allDocumentName);
            input_temp.attr("title", allDocumentName);
            $(".input_input").each(function (index, elem) {
                var $elem = $(elem);
                $elem.removeClass("input_checked");
            });
        } else {
            layer.msg("请选择文件夹！");
        }
    } else {
        $(".input_input").each(function (index, elem) {
            var $elem = $(elem);
            if ($elem.val() == "" && $elem.attr("input-type") != "directory") {
                let current_task = $elem.attr("all-where");
                let current_input = $elem.attr("current-where")
                let d_input = parseInt(current_input);
                let d_task = parseInt(current_task);
                if (d_input < one_task_input_num){
                    all_value[d_task]["input"][d_input]["frontOutput"]["frontOutputName"] = allDocumentName;
                } else {
                    all_value[d_task]["data"][d_input-one_task_input_num]["dataValue"] = allDocumentName;
                }
                $elem.val(allDocumentName);
                $elem.attr("title", allDocumentName);
                return false;
            }
        });
    }
}
getMore = function () {
    $("#getMore").remove();
    var document_form = $("#div_document");
    if (DHtml_content.length > (DHtml_index + 1) * 1000) {
        for (var i = DHtml_index * 1000; i < (DHtml_index + 1) * 1000; i++) {
            if (DHtml_content[i].type == "DIRECTORY") {
                document_form.append('<div class="div_getDocument" title="' + DHtml_content[i].pathSuffix + '" onclick="getDocument(\'' + DHtml_content[i].pathSuffix + '\')">' + DHtml_content[i].pathSuffix + '</div>');
            } else {
                document_form.append('<div class="div_choiceDocument" title="' + DHtml_content[i].pathSuffix + '" onclick="choiceDocument(\'' + DHtml_content[i].pathSuffix + '\')">' + DHtml_content[i].pathSuffix + '</div>');
            }
        }
        document_form.append('<div id="getMore" title="继续加载" onclick="getMore()">继续加载...</div>');
        DHtml_index++;
    } else {
        for (var i = DHtml_index * 1000; i < DHtml_content.length; i++) {
            if (DHtml_content[i].type == "DIRECTORY") {
                document_form.append('<div class="div_getDocument" title="' + DHtml_content[i].pathSuffix + '" onclick="getDocument(\'' + DHtml_content[i].pathSuffix + '\')">' + DHtml_content[i].pathSuffix + '</div>');
            } else {
                document_form.append('<div class="div_choiceDocument" title="' + DHtml_content[i].pathSuffix + '" onclick="choiceDocument(\'' + DHtml_content[i].pathSuffix + '\')">' + DHtml_content[i].pathSuffix + '</div>');
            }
        }
    }
}
createDHtml = function (jsonData) {
    var document_form = $("#div_document");
    DHtml_all = jsonData.length;
    document_form.html("");
    document_form.append('<div class="div_getDocument" onclick="returnPrevious()">../</div>');
    if (DHtml_all > 1000) {
        DHtml_content = jsonData;
        for (var i = 0; i < 1000; i++) {
            if (jsonData[i].type == "DIRECTORY") {
                document_form.append('<div class="div_getDocument" title="' + jsonData[i].pathSuffix + '" onclick="getDocument(\'' + jsonData[i].pathSuffix + '\')">' + jsonData[i].pathSuffix + '</div>');
            } else {
                document_form.append('<div class="div_choiceDocument" title="' + jsonData[i].pathSuffix + '" onclick="choiceDocument(\'' + jsonData[i].pathSuffix + '\')">' + jsonData[i].pathSuffix + '</div>');
            }
        }
        document_form.append('<div id="getMore" title="继续加载" onclick="getMore()">继续加载...</div>');
        DHtml_index++;
    } else {
        for (var i = 0; i < jsonData.length; i++) {
            if (jsonData[i].type == "DIRECTORY") {
                document_form.append('<div class="div_getDocument" title="' + jsonData[i].pathSuffix + '" onclick="getDocument(\'' + jsonData[i].pathSuffix + '\')">' + jsonData[i].pathSuffix + '</div>');
            } else {
                document_form.append('<div class="div_choiceDocument" title="' + jsonData[i].pathSuffix + '" onclick="choiceDocument(\'' + jsonData[i].pathSuffix + '\')">' + jsonData[i].pathSuffix + '</div>');
            }
        }
    }
}

function checkInt(n, max) {
    var regex = /^\d+$/;
    if (regex.test(n)) {
        if (n < max && n > 0) {
        } else {
            layer.alert("请输入1-100的正整数！");
            $("#otherPriority").val(50);
        }
    } else {
        layer.alert("请输入1-100的正整数！");
        $("#otherPriority").val(50);
    }
}

$(function () {
    paginationInit();
});
var paginationMaxLength = 8;/*分页栏的最大显示条数*/
var onlyOnePageIsShow = true;/*只有一页的时候是否显示*/
function paginationInit() {
    $('[am-pagination =pagination_new ]').each(function () {
        initPagination($(this));
    });
}

function initPagination(element) {
    element.html('');
    var pagenumber = element.attr('pagenumber');
    var totalpage = element.attr('totalpage');
    var pMaxLength = element.attr('paginationMaxLength');
    var onePageIsShow = element.attr('onlyOnePageIsShow');/* console.log(totalpage)*/
    if (isNeedPagination(Number(totalpage), onePageIsShow)) {
        var content = '<ul class="am-pagination am-pagination-centered"><li value="0"><a href="javascript:void(0);">«</a></li>';
        for (var i = 1; i <= totalpage; i++) {
            content += '<li value="' + i + '"><a href="javascript:void(0);">' + i + '</a></li>'
        }
        content += '<li value="-1"><a href="javascript:void(0);">»</a></li></ul>';
        element.append(content);
        element.children('ul').children('li[value=' + pagenumber + ']').attr('class', 'am-active');
        setDisplayMaxLength(element, pMaxLength);
        addClickListener(element);
    }
}

function setDisplayMaxLength(element, len) {
    var currentpage = Number(element.attr('pagenumber'));
    var totoalpage = Number(element.attr('totalpage'));
    if (checkParamIsPositiveInteger(len)) {
        len = Number(len);
    } else {
        len = paginationMaxLength;
    }
    if (len < totoalpage) {
        var temp1 = parseInt((len - 1) / 2);
        var temp2 = parseInt(len / 2);
        if (temp1 < temp2) {
            var leftstart = currentpage - temp1;
            var rightend = currentpage + temp1 + 1;
        } else {
            var leftstart = currentpage - temp1;
            var rightend = currentpage + temp1;
        }
        if (leftstart < 1) {
            rightend += (1 - leftstart);
            leftstart = 1;
        }
        if (rightend > totoalpage) {
            if (leftstart > 1) {
                leftstart -= (rightend - totoalpage);
            }
            rightend = totoalpage;
        }
        if (leftstart < 1) {
            leftstart = 1
        }
        while (leftstart > 1) {
            element.children('ul').children('li[value = ' + (--leftstart) + ']').css('display', 'none');
        }
        while (rightend < totoalpage) {
            element.children('ul').children('li[value = ' + (++rightend) + ']').css('display', 'none');
        }
    }
}

function isNeedPagination(totalpage, settingfromHTML) {
    var condition;
    if (settingfromHTML == "true") {
        condition = true;
    } else if (settingfromHTML == 'false') {
        condition = false;
    } else {
        condition = onlyOnePageIsShow;
    }
    if (condition && totalpage < 1) {
        return false;
    } else if (!condition && totalpage <= 1) {
        return false;
    }
    return true;
}/*根据页面pagenumber、my_totoalpage更新分页，参数element传的是分页的容器*/
function addClickListener(element) {
    element.children('ul').children('li').bind('click', function () {
        var temp = Number($(this).attr('value'));
        var pagenumber = Number($(this).parent().parent().attr('pagenumber'));
        var totalpage = Number($(this).parent().parent().attr('totalpage'));
        if (temp == 0) {
            temp = pagenumber - 1;
        } else if (temp == -1) {
            temp = pagenumber + 1;
        }
        if (temp != pagenumber && temp != 0 && temp <= totalpage) {
            $(this).parent().parent().attr('pagenumber', temp);
            paginationClick(element.attr("id"));
            initPagination(element);
        }
        return false;
    })
}

function checkParamIsPositiveInteger(param) {
    var reg = /^[1-9]+[0-9]*]*$/;
    return reg.test(param);
}/*用户需要自己实现的点击事件，参数为分页容器的id*/
function paginationClick(pagination_id) {
    var pagenumber = $('#' + pagination_id + '').attr('pagenumber');
    addBox(pagenumber, 10);
}

function addBox(page_num, num) {
    page_num = page_num - 1;
    $("tbody").remove('.table_body');
    let current_num = 0;
    let one_input = "";
    let one_html = "";
    for (let i = page_num * num; i < (page_num + 1) * num && i < all_value.length; i++) {
        one_input = "";
        current_num = 0;
        if (all_value[i].input.length !== 0) {
            for (let x = 0; x < all_value[i].input.length; x++) {
                let current_input = all_value[i].input[x];
                if (current_num === 0) {
                    one_input += '<tr class="tr_input" type="input"><td class="blockName" blockId="' + current_input.blockName + '" title="' + current_input.blockName + '">' + current_input.blockName + '</td><td class="inputName" title="' + current_input.inputName + '">' + current_input.inputName + '</td><td class="td_input"><div class="am-form-group am-form-icon" style="margin-bottom: 0"><i class="am-close am-close-alt am-close-spin am-icon-times del-icon" style="display: none"></i><input type="text" readonly="readonly" class="input_input" input-type="' + current_input.type + '" all-where="' + i + '" current-where="' + current_num + '" value="' + current_input.frontOutput.frontOutputName + '"/></div></td><td class="taskCtr"><button type="button" class="am-btn am-btn-primary am-btn-xs deleteTask" style="padding-top: 3px;padding-bottom: 3px;" onclick="deleteTask(' + i + ')">删除</button></td></tr>';
                    current_num++;
                } else {
                    one_input += '<tr class="tr_input" type="input"><td class="blockName" blockId="' + current_input.blockName + '" title="' + current_input.blockName + '">' + current_input.blockName + '</td><td class="inputName" title="' + current_input.inputName + '">' + current_input.inputName + '</td><td class="td_input"><div class="am-form-group am-form-icon" style="margin-bottom: 0"><i class="am-close am-close-alt am-close-spin am-icon-times del-icon" style="display: none"></i><input type="text" readonly="readonly" class="input_input" input-type="' + current_input.type + '" all-where="' + i + '" current-where="' + current_num + '" value="' + current_input.frontOutput.frontOutputName + '"/></div></td><td class="taskCtr"></td></tr>';
                    current_num++;
                }
            }
        }
        if (all_value[i].data.length !== 0) {
            for (let y = 0; y < all_value[i].data.length; y++) {
                let current_data = all_value[i].data[y];
                let current_dataName = current_data.dataName.split('+')[0];
                if (current_num === 0) {
                    one_input += '<tr class="tr_input tr_data" type="data"><td class="blockName" blockId="' + current_data.dataName + '" title="' + current_data.dataBlockName + '">' + current_data.dataBlockName + '</td><td class="inputName" title="' + current_dataName + '">' + current_dataName + '</td><td class="td_input"><div class="am-form-group am-form-icon" style="margin-bottom: 0"><i class="am-close am-close-alt am-close-spin am-icon-times del-icon" style="display: none"></i><input type="text" readonly="readonly" class="input_input" input-type="' + current_data.type + '" all-where="' + i + '" current-where="' + current_num + '" value="' + current_data.dataValue + '"/></div></td><td class="taskCtr"><button type="button" class="am-btn am-btn-primary am-btn-xs deleteTask" style="padding-top: 3px;padding-bottom: 3px;">删除</button></td></tr>';
                    current_num++;
                } else {
                    one_input += '<tr class="tr_input tr_data" type="data"><td class="blockName" blockId="' + current_data.dataName + '" title="' + current_data.dataBlockName + '">' + current_data.dataBlockName + '</td><td class="inputName" title="' + current_dataName + '">' + current_dataName + '</td><td class="td_input"><div class="am-form-group am-form-icon" style="margin-bottom: 0"><i class="am-close am-close-alt am-close-spin am-icon-times del-icon" style="display: none"></i><input type="text" readonly="readonly" class="input_input" input-type="' + current_data.type + '" all-where="' + i + '" current-where="' + current_num + '" value="' + current_data.dataValue + '"/></div></td><td class="taskCtr"></td></tr>';
                    current_num++;
                }
            }
        }
        one_html = "<tbody class='table_body'>" + one_input + "</tbody>";
        $("#table").append(one_html);
    }
}

function addPageNum(num) {
    $('[am-pagination =pagination_new ]').attr("totalpage", num);
}

function deleteTask(i) {
    if (allowModification == "true") {
        if (task_number == 1) {
            layer.msg("至少需要一个任务！");
        } else {
            if (confirm("确认要删除吗？")) {
                task_number--;
                $("#task_number").val(task_number);
                $("#showTaskNum").html(task_number);
                all_value.splice(i, 1);
                let add_page_num = Math.ceil(all_value.length / 10);
                let c_page_number = $("#test1").attr('pagenumber');
                addPageNum(add_page_num);
                addBox(c_page_number, 10);
                paginationInit();
            } else {
            }
        }
    } else {
        layer.alert("查看模式无法进行该操作！");
    }
}

let cloneObj = function (obj) {
    let newObj = {};
    if (obj instanceof Array) {
        newObj = [];
    }
    for (let key in obj) {
        let val = obj[key];
        //newObj[key] = typeof val === 'object' ? arguments.callee(val) : val; //arguments.callee 在哪一个函数中运行，它就代表哪个函数, 一般用在匿名函数中。
        newObj[key] = typeof val === 'object' ? cloneObj(val): val;
    }
    return newObj;
};